﻿/* 
 * CacheManagerControl.cs
 * 
 * Author - Ankit Gupta and Gaurav Aggarwal
 * 
 * This file acts as a controller to Cache Manager tab.
 * 
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Security;
using System.Diagnostics;

partial class CacheManagerControl : UserControl
{
    // Apply the changes
    private void applyButton_Click(object sender, EventArgs e)
    {
        try
        {
            // Validate whether entered data is in proper format or not
            if (validatedata() == false)
                return;

            // Cell Server Database Directory 
            if ("(undefined)".Equals(cellServDBTextBox.Text))
            {
                clientKey.DeleteValue("CellServDBDir", false);
            }
            else
            {
                clientKey.SetValue("CellServDBDir", cellServDBTextBox.Text, RegistryValueKind.String);
            }

            // Default AFS Cell
            if ("(none)".Equals(defaultCellTextBox.Text))
            {
                serviceParameters.DeleteValue("Cell", false);
            }
            else
            {
                serviceParameters.SetValue("Cell", defaultCellTextBox.Text, RegistryValueKind.String);
            }

            // Root Volume
            if ("root.afs".Equals(rootVolumeTextBox.Text))
            {
                serviceParameters.DeleteValue("RootVolume", false);
            }
            else
            {
                serviceParameters.SetValue("RootVolume", rootVolumeTextBox.Text, RegistryValueKind.String);
            }

            // Root Mount Point
            if ("/afs".Equals(rootMountPointTextBox.Text))
            {
                serviceParameters.DeleteValue("MountRoot", false);
            }
            else
            {
                serviceParameters.SetValue("MountRoot", rootMountPointTextBox.Text, RegistryValueKind.String);
            }

            serviceParameters.SetValue("SysName", sysnamesTextBox.Text, RegistryValueKind.String);


            // Encrypt Communication
            if (encryptCheckBox.Checked)
            {
                serviceParameters.SetValue("SecurityLevel", Convert.ToInt32(encryptCheckBox.Checked), RegistryValueKind.DWord);
            }
            else
            {
                serviceParameters.DeleteValue("SecurityLevel", false);
            }

            // Ansi File Names
            if (ansiFileNamesCheckBox.Checked)
            {
                serviceParameters.SetValue("StoreAnsiFilenames", Convert.ToInt32(ansiFileNamesCheckBox.Checked), RegistryValueKind.DWord);
            }
            else
            {
                serviceParameters.DeleteValue("StoreAnsiFilenames", false);
            }

            // Hide Dot Files
            if (hiddenDotFilesCheckBox.Checked)
            {
                serviceParameters.DeleteValue("HideDotFiles", false);
            }
            else
            {
                serviceParameters.SetValue("HideDotFiles", Convert.ToInt32(hiddenDotFilesCheckBox.Checked), RegistryValueKind.DWord);
            }

            // CallBack Port
            if ("7001".Equals(callbackPortTextBox.Text))
            {
                serviceParameters.DeleteValue("CallBackPort", false);
            }
            else
            {
                serviceParameters.SetValue("CallBackPort", Int32.Parse(callbackPortTextBox.Text), RegistryValueKind.DWord);
            }
        }
        catch (SecurityException)
        {
            MessageBox.Show("You do not have access to apply these settings.", "Access Denied", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    // Cancel the opearations
    private void cancelButton_Click(object sender, EventArgs e)
    {
        Process[] p = Process.GetProcessesByName("mmc");
        p[0].Kill();

    }
    // Error Checking
    private bool validatedata()
    {
        if(defaultCellTextBox.Text.Equals(""))
        {
            String error = "Default Cell Name should be a non-empty string";
            MessageBox.Show(error);
            return false;
        }
        if(rootVolumeTextBox.Text.Equals(""))
        {
            String error = "Root Volume should be a non-empty string";
            MessageBox.Show(error);
            return false;
        }
        if (rootMountPointTextBox.Text.Equals(""))
        {
            String error = "Root Mount Point should be a non-empty string";
            MessageBox.Show(error);
            return false;
        }
        
        if(!checkint(callbackPortTextBox.Text))
        {
            String error = "CallBack Port should be an integer";
            MessageBox.Show(error);
            return false;
        }
        return true;
    }
        
    private bool checkint(string a)
    {
        bool flag = true;

        if (a.Equals("")) flag = false;

        for (int i = 0; i < a.Length; i++)
        {
            if (a[i] < 48 || a[i] > 57) flag = false;
        }
        return flag;
    }
}